package javaweb.test;

import org.apache.commons.io.IOUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

import static java.net.URLEncoder.*;

/**
 * @author wanglei
 * @create $(YEAR)-$(MONTH)-$(DAY)-$(TIME)
 */

public class FileServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String downFile="1.jpg";
        ServletContext sl = getServletContext();
        String mimeType = sl.getMimeType("/file/" + downFile);
        response.setContentType(mimeType);
        response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode("美女.jpg","utf-8"));
        InputStream rs = sl.getResourceAsStream("/file/" + downFile);
        ServletOutputStream op = response.getOutputStream();
        IOUtils.copy(rs,op);

    }
}
